import {HttpClient} from '@angular/common/http';
import {Injectable} from '@angular/core';
import {catchError, throwError} from "rxjs";
import {environment} from '../../environments/environment';
import {ApiReturnM} from '../modules/ApiReturnM';
import {TokenM} from '../modules/LoginMs';


//封装接口api
@Injectable({
  providedIn: 'root'
})
export class DemejimmerApiservice {

  static readonly baseurl: string = environment.JImmer_BASE_URL;
  static readonly loginUrl: string = "/login";
  static readonly refreshTokenUrl: string = "/refreshToken";
  static readonly permissionQueryallUrl: string = "/permission/queryall";

  constructor(private httpClient: HttpClient) {
  }

  //封装接口api
  login(email: string, password: string) {
    //使用泛型,数组
    return this.httpClient.post<ApiReturnM<TokenM[]>>(
      DemejimmerApiservice.baseurl + DemejimmerApiservice.loginUrl,
      {email, password}
    ).pipe(
      catchError(err => throwError(err))
    )
  }

  //封装接口api
  refreshToken(tokenL: TokenM) {
    return this.httpClient.post<ApiReturnM<TokenM[]>>(
      DemejimmerApiservice.baseurl + DemejimmerApiservice.refreshTokenUrl, {},
      {
        headers: {token: tokenL.jwtToken}
      }
    ).pipe(
      catchError(err => throwError(err))
    )
  }

  //封装接口api
  permissionQueryall() {
    return this.httpClient.post<ApiReturnM<TokenM[]>>(
      DemejimmerApiservice.baseurl + DemejimmerApiservice.permissionQueryallUrl, {}
    ).pipe(
      catchError(err => throwError(err))
    )
  }

}
